package com.maomao.es.vector;

import org.elasticsearch.action.explain.ExplainRequest;
import org.elasticsearch.action.explain.ExplainResponse;
import org.elasticsearch.client.ElasticsearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author mao
 * @date 2025-05-30- 14:54
 * @descript
 */
@RestController
@RequestMapping("/api/vector")
public class VectorSearchController {

    @Autowired
    private VectorDocumentRepository repository;

    @Autowired
    private VectorizationService vectorizationService;

    @PostMapping
    public VectorDocument createDocument(@RequestBody DocumentRequest request) {
        VectorDocument doc = new VectorDocument();
        doc.setTitle(request.getTitle());
        doc.setContent(request.getContent());

        // 生成向量
        doc.setTitleVector(vectorizationService.textToVector(request.getTitle()));
        doc.setContentVector(vectorizationService.textToVector(request.getContent()));

        return repository.save(doc);
    }

    @GetMapping("/search/similar")
    public Page<VectorDocument> searchSimilar(
            @RequestParam String query,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        float[] queryVector = vectorizationService.textToVector(query);
        return repository.findSimilarByContentVector(queryVector, PageRequest.of(page, size));
    }

    @GetMapping("/search/hybrid")
    public Page<VectorDocument> hybridSearch(
            @RequestParam String query,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        float[] queryVector = vectorizationService.textToVector(query);
        return repository.hybridSearch(query, queryVector, PageRequest.of(page, size));
    }

    // 搜索结果解释
//    @GetMapping("/search/explain/{id}")
//    public String explainSearchResult(
//            @PathVariable String id,
//            @RequestParam String query) {
//
//        float[] queryVector = vectorizationService.textToVector(query);
//
//        ExplainRequest explainRequest = ExplainRequest.of(e -> e
//                .index("vector_docs")
//                .id(id)
//                .query(q -> q
//                        .scriptScore(s -> s
//                                .query(qq -> qq.matchAll(m -> m))
//                                .script(sc -> sc
//                                        .inline(i -> i
//                                                .source("cosineSimilarity(params.query_vector, 'contentVector') + 1.0")
//                                                .params("query_vector", JsonData.of(queryVector))
//                                        )
//                                )
//                        )
//                );
//
//        try {
//            ElasticsearchClient client = ... // 获取Elasticsearch客户端
//            ExplainResponse response = client.explain(explainRequest);
//            return response.explanation().toString();
//        } catch (IOException e) {
//            throw new RuntimeException("Failed to explain search result", e);
//        }
//    }



}
